package com.jstyle.jclife.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jstyle.jclife.R;
import com.jstyle.jclife.model.SleepDetail;
import com.jstyle.jclife.utils.DateUtil;

import java.text.NumberFormat;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuge on 2017/2/25 0025.
 */

public class HeartDetailAdapter extends RecyclerView.Adapter {
    private static final String TAG = "MusicAdapter";
    List<SleepDetail> sleepDetails;
    private final static int VIEWTYPE_Title = 0;
    private final static int VIEWTYPE_Content = 1;

    private boolean isNoData = true;
    int[]thisWeekRange;
    int[]lastWeekRange;
    public  final static int DATA_WEEK=0;
    public  final static int DATA_MONTH=1;
    public  final static int DATA_YEAR=2;
    int dataType=0;
    public HeartDetailAdapter(int[]thisWeekRange,int[]lastWeekRange,int dataType) {
        this.thisWeekRange=thisWeekRange;
        this.lastWeekRange=lastWeekRange;
        this.dataType=dataType;
      //  this.sleepDetails = sleepDetails;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case VIEWTYPE_Content:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_heart_content, parent, false);
                return new ContentViewHolder(view);
            case VIEWTYPE_Title:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_heart_title, parent, false);
                return new ViewHolder(view);
            default:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sleepdetail, parent, false);
                return new ViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)){
            case VIEWTYPE_Content:
                bindData(holder,position);
                break;
            case VIEWTYPE_Title:
                bindTitleData(holder);
                break;
        }

    }

    private void bindTitleData(RecyclerView.ViewHolder holder) {
        ViewHolder viewHolder=(ViewHolder)holder;
        Context context=holder.itemView.getContext();
        String contentTitle="";
        String limitTitle="";
        if(dataType==DATA_WEEK){
            contentTitle=context.getString(R.string.total_week);
            limitTitle=context.getString(R.string.VS_LASTWEEK);
        }else if(dataType==DATA_MONTH){
            contentTitle=context.getString(R.string.total_month);
            limitTitle=context.getString(R.string.VS_LASTMONTH);
        }else  if(dataType==DATA_YEAR){
            contentTitle=context.getString(R.string.total_year);
            limitTitle=context.getString(R.string.VS_LASTYEAR);
        }
        viewHolder.tvSleepDetialContent.setText(contentTitle);
        viewHolder.tvSleepDetialLimit.setText(limitTitle);
    }

    private void bindData(RecyclerView.ViewHolder holder, final int position) {
        Context context=holder.itemView.getContext();
        ContentViewHolder contentViewHolder=(ContentViewHolder)holder;
        int id = context.getResources().getIdentifier("item_heart_range_" + (position-1),
                "drawable", context.getPackageName());
        Drawable drawableLeft = context.getResources().getDrawable(id);
        drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
        contentViewHolder.tvSleepDetialTitle.setCompoundDrawables(drawableLeft, null, null, null);
        contentViewHolder.tvSleepDetialTitle.setText(contentViewHolder.heartArray[position-1]);

        int thisWeekMin=thisWeekRange[position-1];
        int lastWeekMin=lastWeekRange[position-1];
        int limit=thisWeekMin-lastWeekMin;
        String limitContent="";
        Drawable drawableResult = null;
        int textColor=0;
        int textContentColor=0;
        if(limit<0){
            textColor=Color.parseColor("#999999");
            textContentColor=Color.parseColor("#999999");

            limitContent=String.valueOf(DateUtil.getHeartRangeTime(context,Math.abs(limit)));
            drawableResult= context.getResources().getDrawable(position==3?R.drawable.heart_dowm_o:R.drawable.heart_down);
        }else{
            textColor=context.getResources().getColor(R.color.heart_range_color_2);
            textContentColor=context.getResources().getColor(R.color.heart_range_color_2);
            drawableResult=  context.getResources().getDrawable(R.drawable.heart_up_o);
            limitContent=DateUtil.getHeartRangeTime(context,limit);
        }

        drawableResult.setBounds(0, 0, drawableResult.getMinimumWidth(), drawableResult.getMinimumHeight());
        contentViewHolder.tvSleepDetialContent.setTextColor(textContentColor);
        contentViewHolder.tvSleepDetialTitle.setTextColor(textColor);
        contentViewHolder.tvSleepDetialLimit.setTextColor(textColor);
        contentViewHolder.tvSleepDetialLimit.setCompoundDrawables(drawableResult, null, null, null);
        contentViewHolder.tvSleepDetialLimit.setText(limitContent);
        contentViewHolder.tvSleepDetialContent.setText(DateUtil.getHeartRangeTime(context,thisWeekMin));
    }

    private String getContentString(int position, float value, Context context) {
        String content = "";
        switch (position) {
            case 0:
                content = context.getResources().getString(R.string.Value);
                break;
            case 1:
                content = DateUtil.getPointSleepTime(context, value);
                break;
            case 2:
                content = DateUtil.getPointSleepTime(context, value);
                break;
            case 3:
                content = DateUtil.getPointSleepTime(context, value);
                break;

            case 4:
                NumberFormat nf = NumberFormat.getPercentInstance();
                nf.setMinimumFractionDigits(0);
                content = String.format(context.getString(R.string.format_sleepEfficiency), nf.format(value));
                break;
            case 5:
                NumberFormat format = NumberFormat.getNumberInstance();
                format.setMinimumFractionDigits(0);
                content = String.format(context.getString(R.string.format_AverageHeartRate), format.format(value));
                break;

        }
        return content;


    }

    private boolean isLimit(int position, float value) {
        boolean isLimit = true;
        switch (position) {
            case 1:
                if (value < 5.5 * 60 || value > 9 * 60) isLimit = false;
                break;
            case 2:
                if (value < 1 * 60 || value > 2.5 * 60) isLimit = false;
                break;
            case 3:
                if (value < 2.5 * 60 || value > 4.8 * 60) isLimit = false;
                break;
            case 4:
                if (value < 75 / 100f || value > 95 / 100f) isLimit = false;
                break;
            case 5:
                if (value < 55 || value > 80) isLimit = false;
                break;
        }
        return isLimit;
    }


    public void setData(List<SleepDetail> sleepDetails) {
        this.sleepDetails = sleepDetails;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return 6;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sleepDetial_title)
        TextView tvSleepDetialTitle;
        @BindView(R.id.tv_sleepDetial_content)
        TextView tvSleepDetialContent;
        @BindView(R.id.tv_sleepDetial_limit)
        TextView tvSleepDetialLimit;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sleepDetial_title)
        TextView tvSleepDetialTitle;
        @BindView(R.id.tv_sleepDetial_content)
        TextView tvSleepDetialContent;
        @BindView(R.id.tv_sleepDetial_limit)
        TextView tvSleepDetialLimit;
        @BindArray(R.array.heartRange_array)
        String[] heartArray;
        public ContentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    @Override
    public int getItemViewType(int position) {
        int viewType = VIEWTYPE_Content;
        switch (position) {
            case 0:
                viewType = VIEWTYPE_Title;
                break;

        }
        return viewType;
    }
}
